﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace PlanetViewer
{
    public class Camera
    {
        //private Vector2 _pos;
        public Vector2 _target;
        public int _bounds;

        public Matrix ViewPosMatrix;
        public Matrix ProjectionMatrix;
        public Matrix ViewRotMatrix;

        private float _aspect;
        private float _fov;

        public float _ZDist = -100f;
        
        public Camera(Vector2 target, int bounds, float aspect = 1.0f, float fov = MathHelper.PiOver4)
        {
            _target = target;
            _bounds = bounds;
            _aspect = aspect;
            _fov = fov;

            Refresh(target, 0f);
        }

        public void Refresh(Vector2 target, float angle)
        {
            RefreshPos(target);
            ProjectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, _aspect, 1.0f, 100000.0f);
            RefreshRot(angle);
        }

        public void RefreshPos(Vector2 target, float zoom = 1.0f)
        {
            this._target = target;

            float Z = _ZDist / zoom;
            ViewPosMatrix = Matrix.CreateLookAt(new Vector3(_target.X, _target.Y, Z), new Vector3(_target.X, _target.Y, 0), new Vector3(1, 0, 0));
            //ViewPosMatrix = Matrix.CreateLookAt(new Vector3(-400, 0, 400), new Vector3(0, 0, 0), new Vector3(0, 0, 1));
        }

        public void RefreshRot(float angle) { ViewRotMatrix = Matrix.CreateRotationZ(-1 * angle); }

        public Vector2 Target
        {
            get { return _target; }
            set { _target = value; RefreshPos(_target); }
        }
    }
}
